package com.haizhi.crm.recommend.service.imp;

import com.haizhi.crm.biz.dao.HistoryViewDao;
import com.haizhi.crm.biz.model.HistoryViewDo;
import com.haizhi.crm.common.constant.CrmSchema;
import com.haizhi.crm.common.json.JSONUtils;
import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.util.CommonUtils;
import com.haizhi.crm.recommend.constant.RecType;
import com.haizhi.crm.recommend.model.RecQo;
import com.haizhi.crm.recommend.model.RecVo;
import com.haizhi.crm.recommend.service.RecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by yangyijun on 2018/7/4.
 */
@Service
public class CompanyRecentView extends BaseRecService implements RecService {
    public static final GLog LOG = new GLog(CompanyRecentView.class);

    @Override
    public String getName() {
        return RecType.Company.SEARCH_RECENT_VIEW;
    }

    @Autowired
    private HistoryViewDao historyViewDao;

    @Override
    public RecVo doRecommend(RecQo recQo) {
        try {
            List<HistoryViewDo> historyViewDos = historyViewDao.findByEntityTypeAndUserId(CommonUtils.getUserID(), CrmSchema.COMPANY, 5);

            List<Map<String, Object>> companyMap = JSONUtils.toListMap(historyViewDos);
            companyMap.forEach(m -> {
                m.put("object_key", m.get("entityKey" ));
                m.put("_schema", CrmSchema.COMPANY);
            });

            return (RecVo) new RecVo().setPayload(companyMap);
        } catch (Exception e) {
            LOG.error("recommend company history view error!", e);
            return (RecVo) new RecVo().setPayload(new ArrayList<>());
        }
    }
}
